package com.zimmur.service.impl.account;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zimmur.dao.account.IUserCommentDetailDao;
import com.zimmur.model.account.User;
import com.zimmur.model.account.UserComment;
import com.zimmur.model.account.UserCommentDetail;
import com.zimmur.model.goods.Goods;
import com.zimmur.service.IUserService;
import com.zimmur.service.account.IUserCommentDetailService;
import com.zimmur.service.account.IUserCommentService;
import com.zimmur.tools.DateUtils;
import com.zimmur.tools.PageUtil;
@Service("userCommentDetailService")
public class UserCommentDetailServiceImpl implements IUserCommentDetailService{
	private static Logger log = LoggerFactory.getLogger(UserCommentDetailServiceImpl.class);
	@Resource
	private IUserCommentDetailDao userCommentDetailDao;
	@Resource
	private IUserCommentService userCommentService;
	@Resource
	private IUserService userService;
	@Override
	public int addCommentDetail(UserCommentDetail userCommentDetail) {
		UserComment userComment=userCommentService.selectCommentByUserId(userCommentDetail.getUserId());
		User user=userService.getUserInfoByUserId(userCommentDetail.getUserId());
		if(userComment==null || user == null){
			return 0;
		}
		try {
			Integer now=DateUtils.getNowUnixTime();
			userComment.setContent(userCommentDetail.getContent());
			userComment.setUpdateTime(now);
			userCommentService.updateComment(userComment);

			userCommentDetail.setUserIcon(user.getUserIcon());
			userCommentDetail.setNickName(user.getNickName());
			userCommentDetail.setIsReply((short)0);
			userCommentDetail.setCreateTime(now);
			userCommentDetail.setReplyRecId(0L);
			int flag=userCommentDetailDao.insertSelective(userCommentDetail);
			return flag;
		} catch (Exception e) {
			log.error(e.getMessage());
			return 0;
		}
	}
	@Override
	public PageUtil<UserCommentDetail> selectCommentDetailByPage(Integer pageNow, long userId) {
		if(pageNow==0){
			pageNow=1;
		}
		UserComment userComment=userCommentService.selectCommentByUserId(userId);
		Integer total=userCommentDetailDao.countCommentDetailByCommentId(userComment.getCommentId());
		PageUtil<UserCommentDetail> pageUtil=new PageUtil<>();
		pageUtil.setPageNo(pageNow);
		pageUtil.setTotalRecord(total);
		List<UserCommentDetail> userCommentDetails=userCommentDetailDao.selectCommentDetailByCommentId(userComment.getCommentId(), pageUtil.getPageBeginNo(), pageUtil.getPageSize());
		if(userCommentDetails!=null && userCommentDetails.size()>0){
			pageUtil.setItemList(userCommentDetails);
		}
		return pageUtil; 
	}
}
